package cc.noau.coder.bean;

import cc.noau.common.utils.PropertiesUtil;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * 配置参数-数据结构定义
 * 和 mybatis_coder.xml 的结构对应
 */
public class CoderSetting {

    private static Properties properties; //配置文件信息

    // 数据库驱动
    private String driver = "com.mysql.jdbc.Driver"; //当前仅支持mysql
    // 驱动路径
    private String driverPath;
    // 数据库ul
    private String url;
    // 用户名
    private String account;
    // 密码
    private String password;
    // 所属系统模块 core/modules
    private String thePackage;

    private String javaModelPackage;

    private String javaModelVoPackage;

    private String javaInterfacePackage;

    private String mapperXMLPackage;

    private String javaServicePackage;

    private String javaServiceImplPackage;

    private String javaControllerPackage;

    private String javaModelTemplate;

    private String javaQueryModelTemplate;

    private String javaModelConditionTemplate;

    private String javaInterfaceTemplate;

    private String mapperXmlTemplate;

    private String javaServiceTemplate;

    private String javaServiceImplTemplate;

    private String javaControllerTemplate;

    private String templatePath; //模板路径

    private List<SettingRow> settingRows;

    private String generateFileType; // 需要生成的文件,默认 controller,service,impl,mapper,xml,model,condition,query

    private String author;

    private String version;

    private boolean isOk = false;

    public CoderSetting() {
        // 初始化配置文件
        initProperties();
        // 初始化配置
        init();
    }

    public CoderSetting(String configFile) {
        // 初始化配置文件
        initProperties(configFile);
        // 初始化配置
        init();
    }

    private void init(){

        if(properties!=null){

            this.setOk(true);

            String path = getClass().getClassLoader().getResource("lib/mysql-connector-java-5.1.35.jar").getPath();
            try {
                path = URLDecoder.decode(path, "utf-8");
            } catch (Exception e) {
                e.printStackTrace();
            }

            //数据库连接相关
            this.driver = properties.getProperty("coder.driver");
            this.url = properties.getProperty("coder.url");
            this.account = properties.getProperty("coder.username");
            this.password = properties.getProperty("coder.password");
            this.driverPath = path;
            //模板
            this.javaModelTemplate = properties.getProperty("coder.javaModelTemplate");
            this.javaQueryModelTemplate = properties.getProperty("coder.javaQueryModelTemplate");
            this.javaModelConditionTemplate = properties.getProperty("coder.javaModelConditionTemplate");
            this.javaInterfaceTemplate = properties.getProperty("coder.javaInterfaceTemplate");
            this.mapperXmlTemplate = properties.getProperty("coder.mapperXmlTemplate");
            this.javaServiceTemplate = properties.getProperty("coder.javaServiceTemplate");
            this.javaServiceImplTemplate = properties.getProperty("coder.javaServiceImplTemplate");
            this.javaControllerTemplate = properties.getProperty("coder.javaControllerTemplate");

            this.templatePath = properties.getProperty("coder.templatePath");

            //默认值 com.xxx.thePackage.
            this.thePackage = properties.getProperty("coder.thePackage");
            //为xml所放位置
            this.mapperXMLPackage = properties.getProperty("coder.mapperXMLPackage");

            this.generateFileType = properties.getProperty("coder.generateFileType");

            this.settingRows = new ArrayList<>();

            setThePackage(this.thePackage);

            //其它信息补充
            this.setAuthor(properties.getProperty("coder.author"));
            this.setVersion(properties.getProperty("coder.version"));
        }
    }

    public static Properties initProperties() {
        return initProperties(null);
    }

    public static Properties initProperties(String configFile) {
        if(properties == null){
            System.out.println("Get Info...");
            if(configFile==null){
                configFile = "config/coder-config-default.properties";
            }
            PropertiesUtil p = new PropertiesUtil(configFile);
            properties = p.getProperties();
        }
        if(properties == null){
            System.err.println("读取配置文件失败");
        }
        return properties;
    }

    public boolean isOk() {
        return isOk;
    }

    public void setOk(boolean ok) {
        isOk = ok;
    }

    public String getJavaModelVoPackage() {
        return javaModelVoPackage;
    }

    public void setJavaModelVoPackage(String javaModelVoPackage) {
        this.javaModelVoPackage = javaModelVoPackage;
    }

    public String getThePackage() {
        return thePackage;
    }

    public void setThePackage(String thePackage) {
        this.thePackage = thePackage;
        this.setJavaModelPackage(properties.getProperty("coder.packagePrefix") +"."+ thePackage + ".$module$." + properties.getProperty("coder.modelPackageSuffix"));
        this.setJavaModelVoPackage(properties.getProperty("coder.packagePrefix") +"."+ thePackage + ".$module$." + properties.getProperty("coder.voPackageSuffix"));
        this.setJavaInterfacePackage(properties.getProperty("coder.packagePrefix") +"."+ thePackage + ".$module$." + properties.getProperty("coder.mapperPackageSuffix"));
        this.setJavaServicePackage(properties.getProperty("coder.packagePrefix") +"."+ thePackage + ".$module$." + properties.getProperty("coder.servicePackageSuffix"));
        this.setJavaServiceImplPackage(properties.getProperty("coder.packagePrefix") +"."+ thePackage + ".$module$." + properties.getProperty("coder.implPackageSuffix"));
        this.setJavaControllerPackage(properties.getProperty("coder.packagePrefix") +"."+ thePackage + ".$module$." + properties.getProperty("coder.controllerPackageSuffix"));
    }

    public static Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public String getGenerateFileType() {
        return generateFileType;
    }

    public void setGenerateFileType(String generateFileType) {
        this.generateFileType = generateFileType;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getJavaModelPackage() {
        return javaModelPackage;
    }

    public void setJavaModelPackage(String javaModelPackage) {
        this.javaModelPackage = javaModelPackage;
    }

    public String getJavaInterfacePackage() {
        return javaInterfacePackage;
    }

    public void setJavaInterfacePackage(String javaInterfacePackage) {
        this.javaInterfacePackage = javaInterfacePackage;
    }

    public String getMapperXMLPackage() {
        return mapperXMLPackage;
    }

    public void setMapperXMLPackage(String mapperXMLPackage) {
        this.mapperXMLPackage = mapperXMLPackage;
    }

    public List<SettingRow> getSettingRows() {
        return settingRows;
    }

    public void setSettingRows(List<SettingRow> settingRows) {
        this.settingRows = settingRows;
    }

    public String getJavaModelTemplate() {
        return javaModelTemplate;
    }

    public void setJavaModelTemplate(String javaModelTemplate) {
        this.javaModelTemplate = javaModelTemplate;
    }

    public String getJavaInterfaceTemplate() {
        return javaInterfaceTemplate;
    }

    public void setJavaInterfaceTemplate(String javaInterfaceTemplate) {
        this.javaInterfaceTemplate = javaInterfaceTemplate;
    }

    public String getDriverPath() {
        return driverPath;
    }

    public void setDriverPath(String driverPath) {
        this.driverPath = driverPath;
    }

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getMapperXmlTemplate() {
        return mapperXmlTemplate;
    }

    public void setMapperXmlTemplate(String mapperXmlTemplate) {
        this.mapperXmlTemplate = mapperXmlTemplate;
    }

    public String getJavaQueryModelTemplate() {
        return javaQueryModelTemplate;
    }

    public void setJavaQueryModelTemplate(String javaQueryModelTemplate) {
        this.javaQueryModelTemplate = javaQueryModelTemplate;
    }

    public String getJavaModelConditionTemplate() {
        return javaModelConditionTemplate;
    }

    public void setJavaModelConditionTemplate(String javaModelConditionTemplate) {
        this.javaModelConditionTemplate = javaModelConditionTemplate;
    }

    public String getJavaServicePackage() {
        return javaServicePackage;
    }

    public void setJavaServicePackage(String javaServicePackage) {
        this.javaServicePackage = javaServicePackage;
    }

    public String getJavaServiceImplPackage() {
        return javaServiceImplPackage;
    }

    public void setJavaServiceImplPackage(String javaServiceImplPackage) {
        this.javaServiceImplPackage = javaServiceImplPackage;
    }

    public String getJavaServiceTemplate() {
        return javaServiceTemplate;
    }

    public void setJavaServiceTemplate(String javaServiceTemplate) {
        this.javaServiceTemplate = javaServiceTemplate;
    }

    public String getJavaServiceImplTemplate() {
        return javaServiceImplTemplate;
    }

    public void setJavaServiceImplTemplate(String javaServiceImplTemplate) {
        this.javaServiceImplTemplate = javaServiceImplTemplate;
    }

    public String getJavaControllerPackage() {
        return javaControllerPackage;
    }

    public void setJavaControllerPackage(String javaControllerPackage) {
        this.javaControllerPackage = javaControllerPackage;
    }

    public String getJavaControllerTemplate() {
        return javaControllerTemplate;
    }

    public void setJavaControllerTemplate(String javaControllerTemplate) {
        this.javaControllerTemplate = javaControllerTemplate;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getTemplatePath() {
        return templatePath;
    }

    public void setTemplatePath(String templatePath) {
        this.templatePath = templatePath;
    }
}
